﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWeb.ServiceBus.Common;
using MyWeb.ServiceBus.Model;
using MyWeb.ServiceBus.Model.Models;
using MyWeb.ServiceBus.Services;
using System.Collections.Generic;

namespace MyWeb.ServiceBus.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GatewayController : ControllerBase
    {
        private IGatewayService _service;
        public GatewayController(IGatewayService service)
        {
            _service = service;
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>

        [HttpGet]
        public async Task<ApiResult> Get(long serviceId, bool isV = false, bool isG = false)
        {
            List<Model.Dto.GatewayDto> org = new List<Model.Dto.GatewayDto>();
            var entity = new List<Gateway>();
            if (isV)
            {
                entity = await _service.QueryAsync(o => o.ServiceId == serviceId && o.ServiceUri.Length > 0);
            }
            else if (isG)
            {
                entity = await _service.QueryAsync(o => o.ServiceId == serviceId && o.Requst.Length > 0);
            }
            else
            {
                entity = await _service.QueryAsync(o => o.ServiceId == serviceId);
            }
            org = entity.Select(a => new Model.Dto.GatewayDto()
            {
                Id = a.Id,
                ServiceId = a.ServiceId,
                Version = a.Version,
                Status = a.Status,
                LoadBalance = a.LoadBalance,
                ThresholdValue = a.ThresholdValue,
                IsFusing = a.IsFusing,

                ServiceUri = a.ServiceUri,
                ServiceType = a.ServiceType,

                SignType = a.SignType,
                Requst = a.Requst,
                IsMD = a.IsMD,
                IsCheckTime = a.IsCheckTime,

                Requsts = a.Requst.Split(",").ToList(),

                UpdateDate = a.UpdateDate,
                UpdateUserId = a.UpdateUserId,
                CreateDate = a.CreateDate,
                CreateUserId = a.CreateUserId,
            }).ToList();
            return ApiResult.SUCCESS(org);
        }
        [HttpGet]
        public async Task<ApiResult> GetVersions(long serviceId)
        {
            var data = await _service.QueryAsync(g => g.ServiceId == serviceId);

            //foreach (var item in data)
            //{
            //    var i = new
            //    {
            //        item.Id,
            //        item.Version
            //    };
            //    list.Add(i);
            //}
            return ApiResult.SUCCESS(data);
        }
        [HttpPost]
        public async Task<ApiResult> AddVersion(Model.Models.Gateway gat)
        {
            Model.Models.Gateway gateway = new Model.Models.Gateway()
            {
                Id = gat.Id,
                ServiceId = gat.ServiceId,
                Version = gat.Version,
                Status = gat.Status,
                LoadBalance = gat.LoadBalance,// 服务多地址负载策略
                ThresholdValue = gat.ThresholdValue,// 限流阀值(次/分钟) ⁣
                IsFusing = gat.IsFusing,// 是否熔断
            };

            //var list = _db.Queryable<Model.Models.Gateway>().Where().Select(g => g.Version).ToList();
            var list = await _service.QueryAsync(g => g.ServiceId == gateway.ServiceId && g.Id != gateway.Id && g.Version == gateway.Version);
            if (list.Count > 0)
            {
                throw new Exception("版本号重复！");
            }

            long con = 0;
            //根据主键，id = 0 执行插入，否则执行更新
            if(gateway.Id > 0)
            {
                con = await _service.EditAsync(gateway);
            }
            else
            {
                gateway.Id = Snowflake.NextId();
                con = await _service.AddAsync(gateway);
            }
            return ApiResult.SUCCESS(con);
        }
        [HttpPost]
        public async Task<ApiResult> AddUrl(Model.Models.Gateway gat)
        {
            var g = await _service.QueryAsync(g => g.Id == gat.Id);

            var ga = g.First();
            ga.ServiceUri = gat.ServiceUri;
            ga.ServiceType = gat.ServiceType;

            var con = await _service.EditAsync(ga);

            return ApiResult.SUCCESS(con);
        }
        [HttpPost]
        public async Task<ApiResult> AddGateway(MyWeb.ServiceBus.Model.Dto.GatewayDto gat)
        {
            if (gat.Requsts.Count > 0)
            {
                gat.Requst = string.Join(",", gat.Requsts);
            }
            else { gat.Requst = ""; }

            var g = await _service.QueryAsync(g => g.Id == gat.Id);
            var ga = g.First();
            ga.SignType = gat.SignType;
            ga.Requst = gat.Requst;
            ga.IsMD = gat.IsMD;
            ga.IsCheckTime = gat.IsCheckTime;

            var con = await _service.EditAsync(ga);

            return ApiResult.SUCCESS(con);
        }
    }
}
